package cc.comac.mom.datahandlers.entity;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import cc.comac.mom.bean.EquipInWarehouseSpareRelation;
import cc.comac.mom.bean.EquipInWarehouseSpareRelationBean;
import cc.comac.mom.bean.ResultDev;
import cc.comac.mom.dao.entity.EquipInWarehouseSpareRelationDao;
import cc.comac.mom.entity.EquipInWarehouseSpareRelationEntity;
import cc.comac.mom.util.BaseEntityDataUtil;
import cc.comac.mom.util.ResultBean;

@Component
@Transactional
public class EquipInWarehouseSpareRelationDataHandler
		extends ServiceImpl<EquipInWarehouseSpareRelationDao, EquipInWarehouseSpareRelationEntity> {

	@Autowired
	private EquipInWarehouseSpareRelationDao equipInWarehouseSpareRelationDao;
	
	@Autowired
	private BaseEntityDataUtil baseEntityDataUtil;

	public boolean batchInsert(List<EquipInWarehouseSpareRelationEntity> list) {

		boolean saveBatch = false;

		if (list.size() > 0) {
			saveBatch = saveBatch(list, list.size());
		}
		return saveBatch;

	}

	public ResultDev selectBySomeField(EquipInWarehouseSpareRelation equipInWarehouseSpareRelation) {
		ResultDev resultDev = new ResultDev();
		EquipInWarehouseSpareRelationEntity innerData = equipInWarehouseSpareRelation.getInnerData();
		if (innerData.getInWarehouseId() == 0) {
			throw new RuntimeException("无法获取入库信息数据");
		}
		if (equipInWarehouseSpareRelation.getPageNum() == 0) {
			equipInWarehouseSpareRelation.setPageNum(1);
		}
		if (equipInWarehouseSpareRelation.getPageSize() == 0) {
			equipInWarehouseSpareRelation.setPageSize(10);
		}
		if (innerData.getInWarehouseId() == 0) {
			innerData.setInWarehouseId(0);
		}
		equipInWarehouseSpareRelation.setPageNum((equipInWarehouseSpareRelation.getPageNum()-1)*equipInWarehouseSpareRelation.getPageSize());
		List<EquipInWarehouseSpareRelationBean> selectSpareDataByInWarehouseId = equipInWarehouseSpareRelationDao
				.selectSpareDataByInWarehouseId(innerData.getInWarehouseId(),
						equipInWarehouseSpareRelation.getPageNum(), equipInWarehouseSpareRelation.getPageSize());
		int selectCountByInWarehouseId = equipInWarehouseSpareRelationDao.selectCountByInWarehouseId(innerData.getInWarehouseId());
		
		resultDev.setEntityList(selectSpareDataByInWarehouseId);
		double ceil = Math.ceil((double)selectCountByInWarehouseId/equipInWarehouseSpareRelation.getPageSize());
		resultDev.setPageCount((int)ceil);
		resultDev.setTotalQuantity(selectCountByInWarehouseId);
		return resultDev;
	}
	
	public ResultDev selectByInWarehouseId (long inWarehouseId) {
		ResultDev resultDev = new ResultDev();
		if (inWarehouseId == 0) {
			throw new RuntimeException("无法获取入库单数据主键");
		}
		QueryWrapper<EquipInWarehouseSpareRelationEntity> queryWrapper = new QueryWrapper<EquipInWarehouseSpareRelationEntity>();
		queryWrapper.eq("row_status", 1);
		queryWrapper.eq("in_warehouse_id",inWarehouseId);
		List<EquipInWarehouseSpareRelationEntity> selectList = equipInWarehouseSpareRelationDao.selectList(queryWrapper);
		resultDev.setEntityList(selectList);
		return resultDev;
	}
	
	public ResultBean batchDelete(List<EquipInWarehouseSpareRelationEntity> list) {
		ResultBean resultBean = new ResultBean(true);
		ArrayList<EquipInWarehouseSpareRelationEntity> arrayList = new ArrayList<EquipInWarehouseSpareRelationEntity>();
		for (EquipInWarehouseSpareRelationEntity equipInWarehouseSpareRelationEntity : list) {
			EquipInWarehouseSpareRelationEntity equipInWarehouseSpareRelationEntity2 = new EquipInWarehouseSpareRelationEntity();
			equipInWarehouseSpareRelationEntity2.setId(equipInWarehouseSpareRelationEntity.getId());
			equipInWarehouseSpareRelationEntity2.setRowStatus(0);
			baseEntityDataUtil.updateBaseEntityData(equipInWarehouseSpareRelationEntity2);
			arrayList.add(equipInWarehouseSpareRelationEntity2);
		}
		updateBatchById(arrayList, arrayList.size());
		return resultBean;
	}
}
